<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/05 19:58:31 $)
// $RCSfile: global_listing.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


/**
* ###############################################
* PROCESS ONLINE SESSIONS
* ###############################################
*/
$timefetch = DATENOW-60;
$timefetchflush = DATENOW-60;
$id = 0;
$staff = $stafflist = $chatobjects = $chatsessionlist = $transferqueue = $transferlist = $transfertreelist = $proactivequeue = $proactivelist = $staffchatqueue = $staffchatlist = array();

$scount = 0;
$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."sessions` WHERE `sessiontype` IN('". SESSION_VISITOR ."', '". SESSION_WINAPP ."', '". SESSION_CHAT ."', '". SESSION_MONITOR ."') AND `lastactivity` >= '". $timefetch ."';");
while ($dbCore->nextRecord())
{
	if ($dbCore->Record["sessiontype"] == SESSION_WINAPP || $dbCore->Record["sessiontype"] == SESSION_MONITOR)
	{
		$staffid = $dbCore->Record["typeid"];
		$stafflist[] = $staffid;
		$staff[$scount]["staffid"] = $staffid;
		$staff[$scount]["staffname"] = $_SWIFT["staffcache"][$staffid]["fullname"];
		$staff[$scount]["staffusername"] = $_SWIFT["staffcache"][$staffid]["username"];
		$staff[$scount]["isonline"] = "true";
		$staff[$scount]["onlinetime"] = $dbCore->Record["dateline"];
		$staff[$scount]["sessionid"] = $dbCore->Record["sessionid"];
		$staff[$scount]["status"] = iif($staffid==$_SWIFT["staff"]["staffid"], $_POST["status"], $dbCore->Record["status"]);
		$scount++;
	} else if ($dbCore->Record["sessiontype"] == SESSION_CHAT) {
		$chatobjectids[] = $dbCore->Record["typeid"];
		$chatsessionlist[$dbCore->Record["typeid"]] = $dbCore->Record["sessionid"];
	} else if ($dbCore->Record["sessiontype"] == SESSION_VISITOR) {
		$id++;
		$sessionids .= ",'". $dbCore->Record["sessionid"] ."'";
	}

	if ($dbCore->Record["lastactivity"] <= $timefetchflush && $flushexec != true && ($dbCore->Record["sessiontype"] == SESSION_CHAT || $dbCore->Record["sessiontype"] == SESSION_VISITOR))
	{
		$flushexec = true;
		if (defined("USESHUTDOWNFUNC"))
		{
			register_shutdown_function("flushVisitors");
		} else {
			flushVisitors();
		}
	}
}

/**
* ###############################################
* PROCESS CHAT OBJECTS
* ###############################################
*/
$chatobjects = $pendingchatvisitorsessionlist = $pendingchatlist = $pendingvisitordata = $inchatvisitorsessionlist = $inchatvisitordata = array();

$chatcount = $pendingchatcount = 0;
$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."chatobjects` WHERE `chatobjectid` IN(". buildIN($chatobjectids) .");");
while ($dbCore->nextRecord())
{
	$chatobjects[count($chatobjects)] = $dbCore->Record;
	if ($dbCore->Record["chatstatus"] == CHAT_INCOMING && $dbCore->Record["staffid"] == $_SWIFT["staff"]["staffid"])
	{
		// pending chats
		$pendingchatvisitorsessionlist[] = $dbCore->Record["visitorsessionid"];
		$pendingvisitordata[$dbCore->Record["visitorsessionid"]]["userfullname"] = $dbCore->Record["userfullname"];
		$pendingvisitordata[$dbCore->Record["visitorsessionid"]]["chatsessionid"] = $chatsessionlist[$dbCore->Record["chatobjectid"]];
		$pendingvisitordata[$dbCore->Record["visitorsessionid"]]["departmenttitle"] = $dbCore->Record["departmenttitle"];

		$pendingchatlist[$pendingchatcount]["chatid"] = $chatsessionlist[$dbCore->Record["chatobjectid"]];
		$pendingchatlist[$pendingchatcount]["displayname"] = $dbCore->Record["userfullname"];
		$pendingchatlist[$pendingchatcount]["chatsessionid"] = $chatsessionlist[$dbCore->Record["chatobjectid"]];
		$pendingchatlist[$pendingchatcount]["visitorsessionid"] = $dbCore->Record["visitorsessionid"];

		$pendingchatcount++;
	} else if ($dbCore->Record["chatstatus"] == CHAT_INCHAT) {
		// in chat status
		$inchatvisitorsessionlist[] = $dbCore->Record["visitorsessionid"];
		$inchatvisitordata[$dbCore->Record["visitorsessionid"]]["userfullname"] = $dbCore->Record["userfullname"];
		$inchatvisitordata[$dbCore->Record["visitorsessionid"]]["chatsessionid"] = $chatsessionlist[$dbCore->Record["chatobjectid"]];
		$inchatvisitordata[$dbCore->Record["visitorsessionid"]]["staffname"] = $dbCore->Record["staffname"];
		$inchatvisitordata[$dbCore->Record["visitorsessionid"]]["departmenttitle"] = $dbCore->Record["departmenttitle"];
	}

	// Is this a staff to staff chat?
	if ($dbCore->Record["chattype"] == CHAT_STAFF && $dbCore->Record["staffid"] == $_SWIFT["staff"]["staffid"] && $dbCore->Record["chatstatus"] == CHAT_INCOMING)
	{
		$squeuecount = count($staffchatqueue);
		$staffchatqueue[$squeuecount]["chatobjectid"] = $dbCore->Record["chatobjectid"];
		$staffchatqueue[$squeuecount]["visitorsessionid"] = $dbCore->Record["visitorsessionid"];
		$staffchatqueue[$squeuecount]["chatsessionid"] = $chatsessionlist[$dbCore->Record["chatobjectid"]];
		$staffchatqueue[$squeuecount]["caption"] = $dbCore->Record["userfullname"];
		$staffchatqueue[$squeuecount]["type"] = "staff";
		$staffchatlist[] = $dbCore->Record["chatobjectid"];
	}

	// Is this a proactive chat?
	if ($dbCore->Record["isproactive"] == "1")
	{
		$pqueuecount = count($proactivequeue);
		$proactivequeue[$pqueuecount]["chatobjectid"] = $dbCore->Record["chatobjectid"];
		$proactivequeue[$pqueuecount]["visitorsessionid"] = $dbCore->Record["visitorsessionid"];
		$proactivequeue[$pqueuecount]["chatsessionid"] = $chatsessionlist[$dbCore->Record["chatobjectid"]];
		$proactivequeue[$pqueuecount]["caption"] = $dbCore->Record["userfullname"];
		$proactivequeue[$pqueuecount]["type"] = "customer";
		$proactivelist[] = $dbCore->Record["chatobjectid"];
	}

	// Are we supposed to initiate a chat over here?
	if ($dbCore->Record["transfertoid"] == $_SWIFT["staff"]["staffid"] && $dbCore->Record["transferstatus"] == TRANSFER_PENDING)
	{
		$tqueuecount = count($transferqueue);
		$transferqueue[$tqueuecount]["chatobjectid"] = $dbCore->Record["chatobjectid"];
		$transferqueue[$tqueuecount]["visitorsessionid"] = $dbCore->Record["visitorsessionid"];
		$transferqueue[$tqueuecount]["chatsessionid"] = $chatsessionlist[$dbCore->Record["chatobjectid"]];
		$transferqueue[$tqueuecount]["caption"] = $dbCore->Record["userfullname"];
		$transferqueue[$tqueuecount]["type"] = "customer";
		$transferlist[] = $dbCore->Record["chatobjectid"];
	}

	// Did we transfer over some chats?
	if ($dbCore->Record["transferfromid"] == $_SWIFT["staff"]["staffid"] && $dbCore->Record["transferstatus"] == TRANSFER_ACCEPTED)
	{
		$tlistcount = count($transfertreelist);
		$transfertreelist[$tlistcount]["chatsessionid"] = $chatsessionlist[$dbCore->Record["chatobjectid"]];
		$transfertreelist[$tlistcount]["visitorsessionid"] = $dbCore->Record["visitorsessionid"];
		$transfertreelist[$tlistcount]["displayname"] = $dbCore->Record["userfullname"];
	}
	
	$chatcount++;
}

/**
* ###############################################
* PROCESS OFFLINE STAFF
* ###############################################
*/
foreach ($_SWIFT["staffcache"] as $key=>$val)
{
	$staffid = $key;
	if (!in_array($staffid, $stafflist))
	{
		$stafflist[] = $staffid;

		$staff[$scount]["staffid"] = $staffid;
		$staff[$scount]["staffname"] = $val["fullname"];
		$staff[$scount]["staffusername"] = $val["username"];
		$staff[$scount]["isonline"] = "false";
		$staff[$scount]["onlinetime"] = "0";
		$staff[$scount]["sessionid"] = "";
		$staff[$scount]["status"] = STATUS_OFFLINE;
		$scount++;
	}
}

/**
* ###############################################
* PROCESS VISITOR FOOTPRINTS
* ###############################################
*/

$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."visitorfootprints` WHERE `sessionid` IN('0'".$sessionids.") ORDER BY `dateline` DESC;");
$sessiondata = array();
$footprints = array();
$sessions = array();
$iplastactivity = array();
$fpcount = 0;
while ($dbCore->nextRecord()) {
	$fpcount++;
	/**
	* Insert Global Data
	*/
	if (!in_array($dbCore->Record["sessionid"], $sessions))
	{
		$sessions[] = $dbCore->Record["sessionid"];		
	}

	// Makes it easy to reference
	$sessionref = &$sessiondata[$dbCore->Record["sessionid"]];

	if (trim($sessionref["sessionid"]) == "") {
		/**
		* This is all static content, it will be same for all pages and we update it only once per session
		*/

		$sessionref["sessionid"] = $dbCore->Record["sessionid"];
		$sessionref["hostname"] = $dbCore->Record["hostname"];
		$sessionref["countryname"] = $dbCore->Record["country"];
		$sessionref["countrycode"] = $dbCore->Record["countrycode"];
		$sessionref["browsername"] = $dbCore->Record["browsername"];
		$sessionref["browserversion"] = $dbCore->Record["browserversion"];
		$sessionref["browsercode"] = $dbCore->Record["browsercode"];
		$sessionref["resolution"] = $dbCore->Record["resolution"];
		$sessionref["platform"] = $dbCore->Record["platform"];
		$sessionref["operatingsys"] = $dbCore->Record["operatingsys"];
		$sessionref["colordepth"] = $dbCore->Record["colordepth"];
		$sessionref["appversion"] = $dbCore->Record["appversion"];
		$sessionref["ipaddress"] = $dbCore->Record["ipaddress"];
		$sessionref["campaignid"] = $dbCore->Record["campaignid"];
		$sessionref["campaigntitle"] = $dbCore->Record["campaigntitle"];
		$sessionref["hasnote"] = $dbCore->Record["hasnote"];
		$sessionref["referrer"] = $dbCore->Record["referrer"];
	}
		
	/**
	* Is current page empty? Set it then, as we fetch by dateline the first record will always be the current page
	*/
	if (trim($sessionref["currentpage"]) == "") {
		$sessionref["currentpage"] = $dbCore->Record["pageurl"];
		$sessionref["pagetitle"] = $dbCore->Record["pagetitle"];
	}
		
	/**
	* Does this record have a search entry? Yep, Add it, Search entry in referrer will only be for single record if there are multiple footprints so we should add it now
	*/
	if (trim($dbCore->Record["searchenginename"]) != "") {
		$sessionref["searchenginename"] = $dbCore->Record["searchenginename"];
		$sessionref["searchstring"] = $dbCore->Record["searchstring"];
	}

	/**
	* Are the color entries not empty? Yeah, Set them too!
	*/
	if (trim($dbCore->Record["rowbgcolor"]) != "") {
		$sessionref["rowbgcolor"] = $dbCore->Record["rowbgcolor"];
	}
	if (trim($dbCore->Record["rowfrcolor"]) != "") {
		$sessionref["rowfrcolor"] = $dbCore->Record["rowfrcolor"];
	}

	/**
	* Does this user has notes attached to him?
	*/
	if ($dbCore->Record["hasnote"] == "1")
	{
		$sessionref["hasnote"] = $dbCore->Record["hasnote"];
	}

	/**
	* Add Footprints
	*/
	$footprintref = &$footprints[$dbCore->Record["sessionid"]];
	$tcount = count($footprintref);
	$footprintref[$tcount]["pagetitle"] = $dbCore->Record["pagetitle"];
	$footprintref[$tcount]["dateline"] = $dbCore->Record["dateline"];
	$footprintref[$tcount]["lastactivity"] = $dbCore->Record["lastactivity"];
	$footprintref[$tcount]["pageurl"] = $dbCore->Record["pageurl"];
	$footprintref[$tcount]["referrer"] = $dbCore->Record["referrer"];
	$sessionlastactivity[$dbCore->Record["sessionid"]][] = $dbCore->Record["lastactivity"];
}

for ($ii=0; $ii<count($sessions); $ii++) {
	$sessiondata[$sessions[$ii]]["min"] = $footprints[$sessions[$ii]][count($footprints[$sessions[$ii]])-1]["dateline"];
	$sessiondata[$sessions[$ii]]["referrer"] = $footprints[$sessions[$ii]][count($footprints[$sessions[$ii]])-1]["referrer"];
	$sessiondata[$sessions[$ii]]["max"] = max($sessionlastactivity[$sessions[$ii]]);
}
?>